rm(list = ls())

###Libraries----
library(dplyr)

###Data----
CCES22 <- read.csv("./replication_hasz/data/CCES22_Common_OUTPUT_vv_topost.csv")
CCES22 <- CCES22[, c("pid3", "votereg", "inputstate")]
CCES22 <- CCES22 %>%
    filter(inputstate == 1 | inputstate == 38 | inputstate == 39) %>%
    filter(votereg == 1)
CCES20 <- read.csv("./replication_hasz/data/CES20_Common_OUTPUT_vv.csv")
CCES20 <- CCES20[, c("pid3", "votereg", "inputstate")]
CCES20 <- CCES20 %>%
  filter(inputstate == 1 & votereg == 1)
CCES18 <- read.csv("./replication_hasz/data/cces18_common_vv.csv")
CCES18 <- CCES18[, c("pid3", "votereg", "inputstate")]
CCES18 <- CCES18 %>%
  filter(inputstate == 38 & votereg == 1)

###Labeling----
CCES22$pid3 <- factor(CCES22$pid3,
                            levels = c(2,3,4,1,5),
                            labels = c("Republican", "Other", "Other", 
                                       "Democrat", "Not Sure"))
CCES20$pid3 <- factor(CCES20$pid3,
                      levels = c(2,3,4,1,5),
                      labels = c("Republican", "Other", "Other", 
                                 "Democrat", "Not Sure"))
CCES18$pid3 <- factor(CCES18$pid3,
                      levels = c(2,3,4,1,5),
                      labels = c("Republican", "Other", "Other", 
                                 "Democrat", "Not Sure"))

CCES22$inputstate <- factor(CCES22$inputstate,
                            levels = c(1,38, 39),
                            labels = c("Alabama", "North Dakota", "Ohio"))
CCES20$inputstate <- factor(CCES20$inputstate,
                            levels = c(1),
                            labels = c("Alabama"))

CCES18$inputstate <- factor(CCES18$inputstate,
                            levels = c(38),
                            labels = c("North Dakota"))

###Table----
CCES22 <- CCES22 %>% arrange(pid3)
CCES20 <- CCES20 %>% arrange(pid3)
CCES18 <- CCES18 %>% arrange(pid3)

prop22 <- with(CCES22, table(inputstate, pid3)) %>%
  prop.table(margin = 1) 
prop20 <- with(CCES20, table(inputstate, pid3)) %>%
  prop.table(margin = 1) 
prop18 <- with(CCES18, table(inputstate, pid3)) %>%
  prop.table(margin = 1) 

print(prop22) #Survey Year for all, Referendum Year for Ohio
print(prop20) #Referendum Year for Alabama
print(prop18) #Referendum Year for North Dakota

